package com.youdianstar.app.entity

import com.youdianstar.app.util.DataHelperEx
import zion830.com.range_picker_dialog.TimeRange

data class OpenItem(
    /*星期单位: 1-6, 周日 0*/
    var value: Int,
    /*是否全天*/
    var allDay: Boolean,
    /*是否开启*/
    var check: Boolean,

    var rangeTime1: MutableList<String>?,
    var rangeTime2: MutableList<String>?,
    var rangeTime3: MutableList<String>?,
    var rangeTime4: MutableList<String>?,

    var partList:MutableList<TimeRange>? =null
){
    fun initPartList(){
        if (null == partList)
            partList = mutableListOf()
        partList?.clear()
        if (!rangeTime1.isNullOrEmpty()){
            partList?.add(DataHelperEx.getRangeByStr(rangeTime1))
        }
        if (!rangeTime2.isNullOrEmpty() && partList?.size!! ==1){
            partList?.add(DataHelperEx.getRangeByStr(rangeTime2))
        }
        if (!rangeTime3.isNullOrEmpty() && partList?.size!! ==2){
            partList?.add(DataHelperEx.getRangeByStr(rangeTime3))
        }
        if (!rangeTime4.isNullOrEmpty() && partList?.size!! ==3){
            partList?.add(DataHelperEx.getRangeByStr(rangeTime4))
        }
    }

    fun removePart(index: Int){
        partList?.let {
            if (it.size>index){
                it.removeAt(index)
            }
        }
    }

    fun addPartItem(index: Int,range : TimeRange){
        partList?.let {
            if (it.size>index){
                it[index] = range
            }else{
                it.add(index,range)
            }
        }
    }

    fun getDefaultRange() = TimeRange(0,0,0,0)

    /*解析获取区间*/

    fun getIndexRange(index:Int):TimeRange{
        return when(index){
            0-> if (partList?.isNotEmpty() == true) partList?.get(index)!! else DataHelperEx.getRangeByStr(rangeTime1)
            1-> if (partList?.get(1) !=null )partList?.get(index)!! else DataHelperEx.getRangeByStr(rangeTime2)
            2-> if (partList?.get(2) !=null )partList?.get(index)!! else DataHelperEx.getRangeByStr(rangeTime3)
            3-> if (partList?.get(3) !=null )partList?.get(index)!! else DataHelperEx.getRangeByStr(rangeTime4)
            else-> getDefaultRange()
        }
    }


}